<template>
    <div>
        <el-button type="primary" @click="dialogFormVisible = true" style="margin:10px">添加志愿者</el-button>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column label="序号" type="index" width="150px"> </el-table-column>
            <el-table-column label="学号" prop="stuId"> </el-table-column>
            <el-table-column label="姓名" prop="stuName"> </el-table-column>
            <el-table-column label="年龄" prop="stuAge"> </el-table-column>
            <el-table-column label="联系电话" prop="phone"> 
                <template slot-scope="scope">
                    <el-tag>{{scope.row.phone}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="学院" prop="college"> </el-table-column>
            <el-table-column>
                <template slot-scope="scope">
                    <el-button size="mini" type="danger" @click="handelDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[5, 10, 20, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="tableData.length"
            >
            </el-pagination>
        </div>
        <el-dialog title="添加志愿者" :visible.sync="dialogFormVisible">
            <el-form :model="volun">
                <el-form-item label="学生姓名" :label-width="formLabelWidth">
                    <el-select v-model="volun.stuId" placeholder="请选择学生姓名">
                        <el-option v-for="item in stuList" :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学院信息" :label-width="formLabelWidth">
                    <el-select v-model="volun.collegeId" placeholder="请选择学院">
                        <el-option v-for="item in collegeList" :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addVolun">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            formLabelWidth: '80px',
            tableData: [],
            currentPage: 1,
            pageSize: 10,
            volun: {
                stuId: '',
                collegeId: ''
            },
            dialogFormVisible: false,
            // 全部学生
            stuList: [],
            // 全部学院信息
            collegeList: []
        };
    },
    methods: {
        async volunteerList() {
            const res = await this.getJSON(`/volunteer/findall?current=${this.currentPage}&size=${this.pageSize}`);
            console.log(res);
            this.tableData = res.data.records;
        },
        async handelDelete(index, row) {
            await this.deleteJSON(`/volunteer/deletebyid?stuId=${row.stuId}`);
            // 删除后重新加载
            this.volunteerList();
        },
        // 加载全部学生信息
        async getStuList() {
            const res = await this.getJSON(`/student/findlist?current=${this.currentPage}&size=${this.pageSize}`);
            this.stuList = res.data.records.filter(item => item.role != 1);
        },
        // 加载全部学院信息
        async getCollegeList() {
            const res = await this.getJSON(`/college/findall?current=${this.currentPage}&size=${this.pageSize}`);
            this.collegeList = res.data.records;
        },
        // 当前页
        handleSizeChange: function(size) {
            this.pagesize = size;
            console.log(this.pagesize); //每页下拉显示数据
        },
        handleCurrentChange: function(currentPage) {
            this.currentPage = currentPage;
            console.log(this.currentPage); //点击第几页
        },
        // 弹出层
        async addVolun() {
            await this.postJSON(`/volunteer/addvolun`, this.volun);
            this.volunteerList();
            this.dialogFormVisible = false;
        }
    },
    created() {
        this.volunteerList();
        this.getStuList();
        this.getCollegeList();
    }
};
</script>
<style scoped></style>
